﻿using ImageGenerator.ViewModels.Pages;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Wpf.Ui.Controls;
using Size = System.Windows.Size;
using Point = System.Windows.Point;
using Wpf.Ui;
using ImageGenerator.Models;

namespace ImageGenerator.Views.Pages
{
    public partial class DashboardPage : INavigableView<DashboardViewModel>
    {
        private readonly ISnackbarService snackbarService;

        public DashboardViewModel ViewModel { get; }

        public DashboardPage(DashboardViewModel viewModel, ISnackbarService snackbarService)
        {
            ViewModel = viewModel;
            this.snackbarService = snackbarService;
            DataContext = this;

            InitializeComponent();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in icList.Items)
            {
                var itemModel = item as DataDto;
                var container = FindContainerForItem(itemModel);
                if (container != null)
                {
                    // 渲染元素到位图
                    var dpi = 96.0; // 可以根据需要调整

                    RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)container.ActualWidth * 2, (int)container.ActualHeight * 2, dpi, dpi, PixelFormats.Pbgra32);
                    DrawingVisual drawingVisual = new();
                    using (DrawingContext drawingContext = drawingVisual.RenderOpen())
                    {
                        VisualBrush vb = new(container);
                        drawingContext.DrawRectangle(vb, null, new Rect(new Point(), new Size(container.ActualWidth * 2, container.ActualHeight * 2)));
                    }

                    renderBitmap.Render(drawingVisual);

                    // 将位图保存为PNG文件
                    PngBitmapEncoder encoder = new();
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Images");
                    if (!File.Exists(filePath))
                        Directory.CreateDirectory(filePath);
                    var fullPath = Path.Combine(filePath, $"{itemModel.Title}_{itemModel.Step}_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png");

                    using FileStream stream = new(fullPath, FileMode.Create);
                    encoder.Save(stream);
                }
            }

            snackbarService.Show("提示", "图片保存成功", ControlAppearance.Success, new SymbolIcon(SymbolRegular.CheckmarkStarburst24), TimeSpan.FromSeconds(3));
        }

        private FrameworkElement FindContainerForItem(DataDto item)
        {
            var generator = icList.ItemContainerGenerator;
            var container = generator.ContainerFromItem(item) as FrameworkElement;

            if (container == null)
            {
                icList.UpdateLayout();
                container = generator.ContainerFromItem(item) as FrameworkElement;
            }

            return container;
        }
    }
}
